from service.Message import Message,CodeMessageData
class TokenHelper():
    def __init__(self):
        self.__token=[]
        self.__jobNumberDic={}
        self.__privilegeDic={}
        self.__idDic={}
        print("tokenHelper init")

    def addToken(self,token,jobNumber,data):
        """
        add token and relation infomation
        :param token:
        :param userId:
        :param data:privilege,id
        :return:
        """
        code = 0000
        try:
            index = self.__token.index(token)
        except ValueError as value:
            self.__token.append(token)
            self.__jobNumberDic[token]=jobNumber
            self.__privilegeDic[token]=data[0]
            self.__idDic[token] = data[1]
        return Message(code,CodeMessageData[code])


    def updateToken(self, oldToken, newToken, jobNumber, privilege):
        """
        update token
        :param oldToken: old token
        :param newToken: updated token
        :return: Message object
        """
        try:
            code=0000
            index=self.__token.index(oldToken)
            self.__token[index]=newToken
            del self.__jobNumberDic[oldToken]
            del self.__privilegeDic[oldToken]
            self.__jobNumberDic[newToken] = jobNumber
            self.__privilegeDic[newToken] = privilege
        except ValueError as value:
            code=1031
            # print(value)
        return Message(code,CodeMessageData[code])

    def deleteToken(self,oldToken):
        """
        delete token
        :param oldToken:
        :return:
        """
        try:
            code = 0000
            index = self.__token.index(oldToken)
            del self.__token[index]
            del self.__jobNumberDic[oldToken]
            del self.__privilegeDic[oldToken]
            del self.__idDic[oldToken]
        except:
            code=1032
        return Message(code, CodeMessageData[code])

    def deleteTokenByTokenList(self, oldTokenList):
        """
        delete token
        :param oldToken:
        :return:
        """
        try:
            code = 0000
            for oldToken in oldTokenList:
                index = self.__token.index(oldToken)
                del self.__token[index]
                del self.__jobNumberDic[oldToken]
                del self.__privilegeDic[oldToken]
                del self.__idDic[oldToken]
        except:
            code = 1032
        return Message(code, CodeMessageData[code])

    def cleanAllToken(self):
        """
        clean all token
        :return: Message object
        """
        try:
            del self.__token
            del self.__jobNumberDic
            del self.__privilegeDic
            del self.__idDic
            self.__token=[]
            self.__jobNumberDic={}
            self.__privilegeDic={}
            self.__idDic={}
            code=0000
        except:
            code=1033
        return Message(code, CodeMessageData[code])


    def verifyToken(self,token):
        """
        verify token
        :param token: token
        :return: Message object
        """
        try:
            code = 0000
            index = self.__token.index(token)
        except ValueError as value:
            code = 1031
        return Message(code, CodeMessageData[code])

    def verifyAdmin(self, token):
        if(self.__privilegeDic[token]==1):
            code=0000
        else:
            code=1022
        return Message(code,CodeMessageData[code])

    def verifyDH(self, token):
        """
        verify Department Head
        :param token:
        :return:
        """
        if(self.__privilegeDic[token]==1 or self.__privilegeDic[token]==2):
            code=0000
        else:
            code=1023
        return Message(code,CodeMessageData[code])

    def getJobNumberBytoken(self,token):
        """
        get job number by token
        :param token:
        :return:
        """
        return  self.__jobNumberDic[token]

    def getPrivilege(self,token):
        """

        :param token:
        :return:
        """
        return self.__privilegeDic[token]

    def deleteTokenByJobNumber(self,jobNumber):
        jobNumberKey=list(self.__jobNumberDic.keys())
        jobNumberValue=list(self.__jobNumberDic.values())
        try:
            tokenList=[]
            code=0000
            try:
                key = jobNumberValue.index(jobNumber)
                tokenList.append(jobNumberKey[key])
                self.deleteTokenByTokenList(tokenList)
            except Exception as ex:
                pass
        except:
            code=1023
        return Message(code,CodeMessageData[code])


    def deleteTokenByJobNumberList(self,jobNumberList):
        jobNumberKey=list(self.__jobNumberDic.keys())
        jobNumberValue=list(self.__jobNumberDic.values())
        try:
            code=0000
            tokenList=[]
            for one in jobNumberList:
                try:
                    keys = jobNumberValue.index(one)
                    tokenList.append(keys)
                except ValueError as va:
                    pass
            self.deleteTokenByTokenList(tokenList)
        except:
            code=1023
        return Message(code,CodeMessageData[code])

    def deleteTokenByIdList(self, idList):
        idKey = list(self.__idDic.keys())
        idValues = list(self.__idDic.values())
        try:
            code = 0000
            tokenList = []
            for one in idList:
                try:
                    key = idValues.index(one)
                    tokenList.append(idKey[key])
                except ValueError as va:
                    pass
            self.deleteTokenByTokenList(tokenList)
        except:
            code = 1023
        return Message(code, CodeMessageData[code])

